'use strict';

module.exports = app => {
    return class StudyService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }

        async list(params) {
            let url = this.app.urls('questionsList');
            let type = params.type;

            if (type == 2){
                url = this.app.urls('easyError');
            }else if (type == 3){
                url = this.app.urls('personError');
            }else if (type == 4){
                url = this.app.urls('randomQuestion');
            }

            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: params,
                timeout: 10000
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async submit(params) {
            let url = this.app.urls('exerciseSubmit');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: params
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }





    }

};
